var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    categoryList: [],
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    showModal:false, // 显示modal弹窗
    goodSpecificationList:[],
    goods:{},
    productList: {},
    checkType:0,
    addGood:{}
  },
  onLoad: function (options) {
    this.getCatalog();
  },
  getCatalog: function () {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    //获取商品类型
    util.request(api.TbCategoryList,{ isOpen: 1 }).then(function (res) {
      console.log(res.data);
      that.setData({
        navList: res.data,
        checkType:res.data[0].id
      });
      that.getGoodsInfo(res.data[0].id)
      wx.hideLoading();
    });

    util.request(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });

  },
  getCurrentCategory: function (id) {
    let that = this;
    util.request(api.CatalogCurrent, { id: id })
        .then(function (res) {
          that.setData({
            currentCategory: res.data.currentCategory
          });
        });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  getList: function () {
    var that = this;
    util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.cat_id)
        .then(function (res) {
          that.setData({
            categoryList: res.data,
          });
        });
  },
  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }
    //现改为获取具体商品信息
    that.getGoodsInfo(event.currentTarget.dataset.id);
  },
  //获取具体商品信息
  getGoodsInfo:function (id){
    let that = this;
    util.request(api.TbGoodsList, {category_id: id, pageIndex: 1, pageSize: 100}).then(function (res) {
      that.setData({
        checkType:id,
        currentCategory:res.data
      });
    });
  },
  //点击选规格 
  clickGood:function(e){
    let that = this;
    util.request(api.TbGoodsSpe, { id: e.currentTarget.dataset.id }).then(function (res) {
      console.log(res.data)
      that.setData({
        showModal:true,
        goodSpecificationList:res.data.goodSpe,
        // productList: res.data.productList,
        goods:res.data.good,
      });
    });
  },
  // 选中类型，修改样式
  selectGoodsType:function(e){
    let that = this;
    let checkedid = e.currentTarget.dataset.id;
    let listid = e.currentTarget.dataset.index;
    let goodsId=e.currentTarget.dataset.goodsid;
    let only=e.currentTarget.dataset.only;
    let goodSpeList=that.data.goodSpecificationList;
    let good=that.data.goods;
    console.log(goodSpeList);
    goodSpeList.forEach(function(item,index){
      if(item.id==goodsId){
        item.fixType[listid].fixs.forEach(function(iitem,iindex){
          if(only==1){//单选
            if(iitem.checked==1){//如果原来是选中的，修改总金额
              good.sale_price-=iitem.sale_price;
            }
            iitem.checked=0;
            if(checkedid==iitem.id){
              iitem.checked = 1
              good.sale_price+=iitem.sale_price;
            }
          }else{
            if(checkedid==iitem.id){
              if(iitem.checked==1){
                iitem.checked=0
                good.sale_price-=iitem.sale_price;
              }else{
                iitem.checked=1
                good.sale_price+=iitem.sale_price;
              }
            }
          }
        });
      }
    });

    that.setData({
      goodSpecificationList:goodSpeList,
      // productList: res.data.productList,
      goods:good,
    });
  },
  /**
   * 添加到购物车
   */
  addToCart: function () {
    var that = this;

    //验证库存
    if (that.data.goods.good_num < this.data.number) {
      wx.showToast({
        title: '提示没有库存'
      });
      return false;
    }
    let datas={};
    datas.goods=that.data.goods;
    datas.goodSpe=that.data.goodSpecificationList;
    //添加到购物车，暂时添加一个
    util.request(api.CartAdd, datas, 'POST', 'application/json')
        .then(function (res) {
          let _res = res;
          if (_res.errno == 0) {
            wx.showToast({
              title: '添加成功'
            });
            that.setData({
              showModal:false
            })
          } else {
            wx.showToast({
              image: '/static/images/icon_error.png',
              title: _res.errmsg,
              mask: true
            });
          }

        });

  },
  //判断规格是否选择完整
  isCheckedAllSpec: function () {
    return !this.getCheckedSpecValue().some(function (v) {
      if (v.valueId == 0) {
        return true;
      }
    });
  },
  getCheckedProductItem: function (key) {
    return this.data.productList.filter(function (v) {
      if (v.goods_specification_ids.indexOf(key) > -1) {
        return true;
      } else {
        return false;
      }
    });
  },
  getCheckedSpecKey: function () {
    let checkedValue = this.getCheckedSpecValue().map(function (v) {
      return v.valueId;
    });
    // return checkedValue.join(',');
    return checkedValue.join('_');
  },
  //获取选中的规格信息
  getCheckedSpecValue: function () {
    let checkedValues = [];
    let _specificationList = this.data.goodSpecificationList;
    for (let i = 0; i < _specificationList.length; i++) {
      let _checkedObj = {
        nameId: _specificationList[i].specification_id,
        valueId: 0,
        valueText: ''
      };
      for (let j = 0; j < _specificationList[i].valueList.length; j++) {
        if (_specificationList[i].valueList[j].checked) {
          _checkedObj.valueId = _specificationList[i].valueList[j].id;
          _checkedObj.valueText = _specificationList[i].valueList[j].value;
        }
      }
      checkedValues.push(_checkedObj);
    }

    return checkedValues;

  },
})
